﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ttg_internal.Utilities.UsageVisualizer.Other.Controls
{
    /// <summary>
    /// Pump graph control
    /// </summary>
    public class PumpGraph : Canvas
    {
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        public PumpGraph()
        {
            Height = 100;
            Width = 75;

            // Initialize transform collection
            var myRotateTransform = new RotateTransform(270, 0, 0);
            var myTranslateTransform = new TranslateTransform(12, 100);
            var transformCollection = new TransformCollection { myRotateTransform, myTranslateTransform };
            var transformGroup = new TransformGroup { Children = transformCollection };

            _rectanglePump = new Rectangle
                                 {
                                     Name = "rectangleAnimation",
                                     Width = _y,
                                     Height = 50,
                                     Opacity = .75,
                                     Fill = Brushes.Green,
                                     RenderTransform = transformGroup
                                 };

            Children.Insert(0, _rectanglePump);
        }

        // Private fields
        private readonly TimeSpan _timeSpan = TimeSpan.FromSeconds(.5);
        private readonly Rectangle _rectanglePump;
        private AnimationClock _clockRectangle;
        private double _currentValue;
        private DoubleAnimation _rectangleAnimationY;
        private double _y;

        // Dependency properties
        public static readonly DependencyProperty PercentageProperty = DependencyProperty.Register("Percentage", typeof(double), typeof(PumpGraph), new PropertyMetadata(0d, PercentageChanged));

        /// <summary>
        /// Percentage to show
        /// </summary>
        public double Percentage
        {
            get { return (double)GetValue(PercentageProperty); }
            set
            {
                SetValue(PercentageProperty, value);
            }
        }

        /// <summary>
        /// Performs animation
        /// </summary>
        private void Do2DAnimation()
        {
            // Add animation to rectangle
            _rectangleAnimationY = new DoubleAnimation(_y * 100, _currentValue * 100, new Duration(_timeSpan));

            // Null out prior animation
            _rectanglePump.ApplyAnimationClock(Rectangle.WidthProperty, null);
            _clockRectangle = _rectangleAnimationY.CreateClock();
            _rectanglePump.ApplyAnimationClock(Rectangle.WidthProperty, _clockRectangle);

            // Insert the line and begin animations
            _clockRectangle.Controller.Begin();
        }

        /// <summary>
        /// Callback that is called when Percentage has changed
        /// </summary>
        private static void PercentageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.Assert(d is PumpGraph);
            var pumpGraph = ((PumpGraph)d);

            Debug.Assert(e.NewValue is double);
            pumpGraph._currentValue = (double)e.NewValue;

            pumpGraph.Do2DAnimation();
            pumpGraph._y = pumpGraph._currentValue;
        }
    }
}


